package com.team9.fitness;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

import java.io.File;

/**
 * Fitness Send 前端接口层主应用类
 * 
 * 功能特性：
 * - 接收前端HTTP请求
 * - 使用ZRDDS将请求数据发送给fitness-get
 * - 接收fitness-get的执行结果并返回给前端
 * 
 * 技术栈：
 * - Spring Boot 3.x
 * - ZRDDS 物联网中间件
 */
@SpringBootApplication(exclude = {
    org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class,
    org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class
})
@EnableAsync
public class FitnessSendApplication {
    public static void main(String[] args) {
        // 设置ZRDDS库路径
        setupZRDDSPath();
        
        SpringApplication.run(FitnessSendApplication.class, args);
    }
    
    /**
     * 设置ZRDDS库路径
     */
    private static void setupZRDDSPath() {
        try {
            // 获取项目根目录
            String projectRoot = System.getProperty("user.dir");
            
            // 尝试多个可能的库路径
            String[] possiblePaths = {
                projectRoot + File.separator + "lib",
                projectRoot + File.separator + ".." + File.separator + "lib",
                projectRoot + File.separator + "fitness-send" + File.separator + ".." + File.separator + "lib",
                "D:" + File.separator + "ZRDDS" + File.separator + "ZRDDS-2.4.4" + File.separator + "lib",
                "E:" + File.separator + "1 study" + File.separator + "Project" + File.separator + "fitness" + File.separator + "lib"
            };
            
            String libraryPath = System.getProperty("java.library.path");
            StringBuilder newLibraryPath = new StringBuilder();
            
            if (libraryPath != null && !libraryPath.isEmpty()) {
                newLibraryPath.append(libraryPath);
            }
            
            boolean found = false;
            for (String path : possiblePaths) {
                File libDir = new File(path);
                if (libDir.exists() && libDir.isDirectory()) {
                    File dllFile = new File(libDir, "ZRDDS_JAVA.dll");
                    if (dllFile.exists()) {
                        if (newLibraryPath.length() > 0) {
                            newLibraryPath.append(File.pathSeparator);
                        }
                        newLibraryPath.append(path);
                        found = true;
                        System.out.println("找到ZRDDS库文件: " + dllFile.getAbsolutePath());
                        break;
                    }
                }
            }
            
            if (found) {
                System.setProperty("java.library.path", newLibraryPath.toString());
                System.out.println("设置ZRDDS库路径: " + newLibraryPath.toString());
            } else {
                System.err.println("警告: 未找到ZRDDS_JAVA.dll文件，请检查库文件路径");
                System.err.println("当前工作目录: " + projectRoot);
                System.err.println("尝试的路径:");
                for (String path : possiblePaths) {
                    System.err.println("  - " + path);
                }
            }
            
        } catch (Exception e) {
            System.err.println("设置ZRDDS库路径时发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
